package com.xl98.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xl98.entity.GroupsMsgContent;
import com.xl98.entity.MsgContent;
import com.xl98.mapper.GroupsMsgContentMapper;
import com.xl98.mapper.MsgContentMapper;
import com.xl98.service.GroupsMsgContentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ls
 * @since 2020-05-15
 */
@Service
public class GroupsMsgContentServiceImpl extends ServiceImpl<GroupsMsgContentMapper, GroupsMsgContent> implements GroupsMsgContentService {
    @Autowired
    private GroupsMsgContentMapper groupsMsgContentMapper;
    @Autowired
    private MsgContentMapper msgContentMapper;

    @Override
    public List<MsgContent> selectByGroupsId(String groupsId) {
        List<MsgContent> list = new ArrayList<>();
        QueryWrapper<GroupsMsgContent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("groups_id",groupsId);
        List<GroupsMsgContent> list1 = groupsMsgContentMapper.selectList(queryWrapper);
        for(GroupsMsgContent groupsMsgContent : list1){
            list.add(msgContentMapper.selectById(groupsMsgContent.getMsgContentId()));
        }
        return list;
    }

    @Override
    public List<MsgContent> selectByGroupsIdUserId(String groupsId, String userId) {
        List<MsgContent> list = new ArrayList<>();
        List<MsgContent> list1 = this.selectByGroupsId(groupsId);
        for(MsgContent msgContent : list1){
            QueryWrapper<MsgContent> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id",msgContent.getId());
            queryWrapper.eq("from_user_id",userId);
            list.add(msgContentMapper.selectOne(queryWrapper));
        }
        return list;
    }

    @Override
    public GroupsMsgContent selectByMsgId(String msgId) {
        QueryWrapper<GroupsMsgContent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("msg_content_id",msgId);
        GroupsMsgContent groupsMsgContent = groupsMsgContentMapper.selectOne(queryWrapper);
        return groupsMsgContent;
    }

    @Override
    public boolean insert(GroupsMsgContent groupsMsgContent) {
        if(groupsMsgContentMapper.insert(groupsMsgContent)>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteByGroupsIdMsgId(String groupsId, String msgId) {
        QueryWrapper<GroupsMsgContent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("groups_id",groupsId);
        queryWrapper.eq("msg_content_id",msgId);
        if(groupsMsgContentMapper.delete(queryWrapper)>0){
            return true;
        }
        return false;
    }


}
